2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_PAINTELEMENTROUNDEDRECTANGLE_JUCEHEADER__
27 #define __JUCER_PAINTELEMENTROUNDEDRECTANGLE_JUCEHEADER__
29 #include "jucer_ColouredElement.h"
32 //==============================================================================
35 class PaintElementRoundedRectangle
: public ColouredElement
38 //==============================================================================
39 PaintElementRoundedRectangle (PaintRoutine
* owner
)
40 : ColouredElement (owner
, "Rounded Rectangle", true, false)
45 ~PaintElementRoundedRectangle() {}
47 //==============================================================================
48 void draw (Graphics
& g
, const ComponentLayout
* layout
, const Rectangle
<int>& parentArea
)
51 position
.getRectangleDouble (x
, y
, w
, h
, parentArea
, layout
);
53 fillType
.setFillType (g
, getDocument(), parentArea
);
54 g
.fillRoundedRectangle ((float) x
, (float) y
, (float) w
, (float) h
, (float) cornerSize
);
58 strokeType
.fill
.setFillType (g
, getDocument(), parentArea
);
60 g
.drawRoundedRectangle ((float) x
, (float) y
, (float) w
, (float) h
, (float) cornerSize
,
61 getStrokeType().stroke
.getStrokeThickness());
65 void getEditableProperties (Array
<PropertyComponent
*>& properties
)
67 properties
.add (new CornerSizeProperty (this));
69 ColouredElement::getEditableProperties (properties
);
71 properties
.add (new ShapeToPathProperty (this));
74 //==============================================================================
75 class SetCornerSizeAction
: public PaintElementUndoableAction
<PaintElementRoundedRectangle
>
78 SetCornerSizeAction (PaintElementRoundedRectangle
* const element
, const double newSize_
)
79 : PaintElementUndoableAction
<PaintElementRoundedRectangle
> (element
),
82 oldSize
= element
->getCornerSize();
88 getElement()->setCornerSize (newSize
, false);
95 getElement()->setCornerSize (oldSize
, false);
100 double newSize
, oldSize
;
103 void setCornerSize (const double newSize
, const bool undoable
)
105 if (newSize
!= cornerSize
)
109 perform (new SetCornerSizeAction (this, newSize
),
110 "Change rounded rectangle corner size");
114 cornerSize
= newSize
;
120 double getCornerSize() const throw() { return cornerSize
; }
122 //==============================================================================
123 void fillInGeneratedCode (GeneratedCode
& code
, String
& paintMethodCode
)
125 if (! fillType
.isInvisible())
127 String x
, y
, w
, h
, s
;
128 positionToCode (position
, code
.document
->getComponentLayout(), x
, y
, w
, h
);
130 fillType
.fillInGeneratedCode (code
, paintMethodCode
);
131 s
<< "g.fillRoundedRectangle ("
132 << castToFloat (x
) << ", "
133 << castToFloat (y
) << ", "
134 << castToFloat (w
) << ", "
135 << castToFloat (h
) << ", "
136 << valueToFloat (cornerSize
) << ");\n\n";
138 paintMethodCode
+= s
;
141 if (isStrokePresent
&& ! strokeType
.isInvisible())
143 String x
, y
, w
, h
, s
;
144 positionToCode (position
, code
.document
->getComponentLayout(), x
, y
, w
, h
);
146 strokeType
.fill
.fillInGeneratedCode (code
, paintMethodCode
);
147 s
<< "g.drawRoundedRectangle ("
148 << castToFloat (x
) << ", "
149 << castToFloat (y
) << ", "
150 << castToFloat (w
) << ", "
151 << castToFloat (h
) << ", "
152 << valueToFloat (cornerSize
) << ", "
153 << valueToFloat (strokeType
.stroke
.getStrokeThickness()) << ");\n\n";
155 paintMethodCode
+= s
;
159 static const char* getTagName() throw() { return "ROUNDRECT"; }
161 XmlElement
* createXml() const
163 XmlElement
* const e
= new XmlElement (getTagName());
165 position
.applyToXml (*e
);
166 e
->setAttribute ("cornerSize", cornerSize
);
167 addColourAttributes (e
);
172 bool loadFromXml (const XmlElement
& xml
)
174 if (xml
.hasTagName (getTagName()))
176 position
.restoreFromXml (xml
, position
);
177 cornerSize
= xml
.getDoubleAttribute ("cornerSize", 10.0);
178 loadColourAttributes (xml
);
192 getCurrentAbsoluteBoundsDouble (x
, y
, w
, h
);
195 path
.addRoundedRectangle ((float) x
, (float) y
, (float) w
, (float) h
, (float) cornerSize
);
197 convertToNewPathElement (path
);
203 //==============================================================================
204 class CornerSizeProperty
: public SliderPropertyComponent
,
205 public ChangeListener
208 CornerSizeProperty (PaintElementRoundedRectangle
* const owner_
)
209 : SliderPropertyComponent ("corner size", 1.0, 200.0, 0.5, 0.4),
212 owner
->getDocument()->addChangeListener (this);
215 ~CornerSizeProperty()
217 owner
->getDocument()->removeChangeListener (this);
220 void setValue (double newValue
)
222 owner
->getDocument()->getUndoManager().undoCurrentTransactionOnly();
224 owner
->setCornerSize (newValue
, true);
227 double getValue() const { return owner
->getCornerSize(); }
229 void changeListenerCallback (ChangeBroadcaster
*) { refresh(); }
232 PaintElementRoundedRectangle
* const owner
;
235 //==============================================================================
236 class ShapeToPathProperty
: public ButtonPropertyComponent
239 ShapeToPathProperty (PaintElementRoundedRectangle
* const element_
)
240 : ButtonPropertyComponent ("path", false),
247 element
->convertToPath();
250 const String
getButtonText() const
252 return "convert to a path";
256 PaintElementRoundedRectangle
* const element
;
261 #endif // __JUCER_PAINTELEMENTROUNDEDRECTANGLE_JUCEHEADER__